package org.dromara.knowledge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.knowledge.domain.bo.KnowledgeSchemaGroupBo;
import org.dromara.knowledge.domain.vo.KnowledgeSchemaGroupVo;
import org.dromara.knowledge.service.KnowledgeSchemaGroupService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库模式组控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/schema/group")
public class KnowledgeSchemaGroupController extends BaseController {

    private final KnowledgeSchemaGroupService knowledgeSchemaGroupService;

    /**
     * 查询知识库模式组列表
     */
    @SaCheckPermission("knowledge:schema:group:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeSchemaGroupVo> list(KnowledgeSchemaGroupBo bo, PageQuery pageQuery) {
        return knowledgeSchemaGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库模式组列表
     */
    @SaCheckPermission("knowledge:schema:group:export")
    @Log(title = "知识库模式组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeSchemaGroupBo bo, HttpServletResponse response) {
        List<KnowledgeSchemaGroupVo> list = knowledgeSchemaGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库模式组数据", KnowledgeSchemaGroupVo.class, response);
    }

    /**
     * 获取知识库模式组详细信息
     *
     * @param groupId 模式组ID
     */
    @SaCheckPermission("knowledge:schema:group:query")
    @GetMapping("/{groupId}")
    public R<KnowledgeSchemaGroupVo> getInfo(@PathVariable Long groupId) {
        return R.ok(knowledgeSchemaGroupService.queryById(groupId));
    }

    /**
     * 新增知识库模式组
     */
    @SaCheckPermission("knowledge:schema:group:add")
    @Log(title = "知识库模式组", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgeSchemaGroupBo bo) {
        return toAjax(knowledgeSchemaGroupService.insertByBo(bo));
    }

    /**
     * 修改知识库模式组
     */
    @SaCheckPermission("knowledge:schema:group:edit")
    @Log(title = "知识库模式组", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgeSchemaGroupBo bo) {
        return toAjax(knowledgeSchemaGroupService.updateByBo(bo));
    }

    /**
     * 删除知识库模式组
     *
     * @param groupIds 模式组ID串
     */
    @SaCheckPermission("knowledge:schema:group:remove")
    @Log(title = "知识库模式组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public R<Void> remove(@PathVariable Long[] groupIds) {
        return toAjax(knowledgeSchemaGroupService.deleteWithValidByIds(List.of(groupIds), true));
    }

    /**
     * 根据ID查询模式组信息
     *
     * @param id ID
     */
    @SaCheckPermission("knowledge:schema:group:query")
    @GetMapping("/id/{id}")
    public R<KnowledgeSchemaGroupVo> getById(@PathVariable Long id) {
        return R.ok(knowledgeSchemaGroupService.queryById(id));
    }

    /**
     * 根据编码查询模式组信息
     *
     * @param code 编码
     */
    @SaCheckPermission("knowledge:schema:group:query")
    @GetMapping("/code/{code}")
    public R<KnowledgeSchemaGroupVo> getByCode(@PathVariable String code) {
        return R.ok(knowledgeSchemaGroupService.queryByCode(code));
    }

    /**
     * 根据名称查询模式组信息
     *
     * @param name 名称
     */
    @SaCheckPermission("knowledge:schema:group:query")
    @GetMapping("/name/{name}")
    public R<KnowledgeSchemaGroupVo> getByName(@PathVariable String name) {
        return R.ok(knowledgeSchemaGroupService.queryByName(name));
    }
}